package org.ydeity.lucene.test.service;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.ydeity.lucene.core.LuceneTemplate;
import org.ydeity.lucene.test.entity.POI;

import java.util.List;
import java.util.Optional;

@Service
public class POIService {

    @Autowired
    private LuceneTemplate luceneTemplate;

    public boolean doInsert(POI poi){
        return luceneTemplate.doInsert(poi);
    }

    public boolean doUpdate(POI poi){
        return luceneTemplate.doUpdate(poi);
    }

    public int saveAll(List<POI> poiList){
        return luceneTemplate.saveAll(poiList,POI.class);
    }

    public boolean doDelete(POI poi){
        // 1. return luceneTemplate.delete(poi);
        // 2. return luceneTemplate.doDelete(POI.class,poi.getId());

        return luceneTemplate.delete(poi);
    }

    public long deleteAll(){
        return luceneTemplate.deleteAll(POI.class);
    }

    public long deleteAll(List<POI> poiList){
        return luceneTemplate.deleteAll(poiList,POI.class);
    }

    public long count(){
        return luceneTemplate.count(POI.class,"id");
    }

    public Optional<POI> getById(Long id){
        return luceneTemplate.getById(POI.class, id);
    }

    public List<POI> queryList(Query query,int n){
        return luceneTemplate.queryForObject(POI.class,query,n);
    }

    public List<POI> queryListAndSort(Query query, Sort sort, int n) {
        return luceneTemplate.queryForObject(POI.class,query,sort,n);
    }

    public Page<POI> page(Query query, PageRequest pageRequest){
        return luceneTemplate.search(POI.class, query, pageRequest);
    }

    public Page<POI> pageAndSort(Query query,PageRequest pageRequest,Sort sort){
        return luceneTemplate.search(POI.class,query,sort,pageRequest);
    }
}
